﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Card.Make.Pro.Application.Helper;

namespace Card.Make.Pro.Form
{
    public partial class FrmApplicationQuery : DevExpress.XtraEditors.XtraForm
    {
        public FrmApplicationQuery()
        {
            InitializeComponent();
            this.txtKeyword.Edit.KeyDown += new KeyEventHandler(Edit_KeyDown);
        }


        void Edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadingHelper.Open();

                var value = (sender as DevExpress.XtraEditors.TextEdit)?.EditValue;
                txtKeyword.EditValue = value;
                Query();
                LoadingHelper.Close();
            }
        }


        SqlHelper sqlHelper = new SqlHelper();

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadingHelper.Open();
            Query();
            LoadingHelper.Close();
        }


        private void Query()
        {
            string CommandText;
            var userInput = this.txtKeyword.EditValue?.ToString();
            if (string.IsNullOrWhiteSpace(userInput))
            {
                CommandText =
                    "Select top 100 [AppVerID] ,convert(varchar(10),[CardMode]) as  CardMode,[CardDetail] ,convert(varchar(10),[Priority]) as  Priority,convert(varchar(10),[State] ) as 'State',[CreateDT],[ModifyDT] ,[EndDT] ,[LockDT] ,convert(varchar(10),[CertificateState]) as CertificateState,[DoHis] from CardApplication_Table  ";
            }
            else
            {
                var where = $"AppVerID like '{userInput}' or CardDetail like '%{userInput}%'";
                if (userInput.Contains("P")|| userInput.Contains("p"))
                    where = $"AppVerID = '{userInput.Replace("P", string.Empty).Replace("p", string.Empty)}'";
                CommandText =
                    "Select [AppVerID] ,convert(varchar(10),[CardMode]) as  CardMode,[CardDetail] ,convert(varchar(10),[Priority]) as  Priority,convert(varchar(10),[State] ) as 'State',[CreateDT],[ModifyDT] ,[EndDT] ,[LockDT] ,convert(varchar(10),[CertificateState]) as CertificateState,[DoHis] from CardApplication_Table where " +
                    where;
            }

            DataTable dt = sqlHelper.GetQueryResult(CommandText);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["State"].ToString())
                {
                    case "0":
                        dt.Rows[i]["State"] = "未知";
                        break;
                    case "1":
                        dt.Rows[i]["State"] = "制证申请";
                        break;
                    case "2":
                        dt.Rows[i]["State"] = "信息录入";
                        break;
                    case "3":
                        dt.Rows[i]["State"] = "证卡打印";
                        break;
                    case "4":
                        dt.Rows[i]["State"] = "证卡授权";
                        break;
                    case "5":
                        dt.Rows[i]["State"] = "证卡检测";
                        break;
                    case "6":
                        dt.Rows[i]["State"] = "身份审核";
                        break;
                    case "7":
                        dt.Rows[i]["State"] = "证卡发放";
                        break;
                    case "8":
                        dt.Rows[i]["State"] = "受理完成";
                        break;
                    case "9":
                        dt.Rows[i]["State"] = "受理中止";
                        break;
                }

                switch (dt.Rows[i]["CardMode"].ToString())
                {
                    case "1":
                        dt.Rows[i]["CardMode"] = "人员";
                        break;
                    case "2":
                        dt.Rows[i]["CardMode"] = "车辆";
                        break;
                    case "3":
                        dt.Rows[i]["CardMode"] = "访客";
                        break;
                }

                switch (dt.Rows[i]["CertificateState"].ToString())
                {
                    case "0":
                        dt.Rows[i]["CertificateState"] = "未知";
                        break;
                    case "1":
                        dt.Rows[i]["CertificateState"] = "通过";
                        break;
                    case "2":
                        dt.Rows[i]["CertificateState"] = "未通过";
                        break;
                }

                switch (dt.Rows[i]["CertificateState"].ToString())
                {
                    case "0":
                        dt.Rows[i]["CertificateState"] = "未知";
                        break;
                    case "1":
                        dt.Rows[i]["CertificateState"] = "通过";
                        break;
                    case "2":
                        dt.Rows[i]["CertificateState"] = "未通过";
                        break;
                }

                switch (dt.Rows[i]["Priority"].ToString())
                {
                    case "0":
                        dt.Rows[i]["Priority"] = "加急处理";
                        break;
                    case "1":
                        dt.Rows[i]["Priority"] = "高";
                        break;
                    case "2":
                        dt.Rows[i]["Priority"] = "中";
                        break;
                    case "3":
                        dt.Rows[i]["Priority"] = "低";
                        break;
                }
            }

            this.gridControl1.DataSource = dt;
        }

        //private void gridControl1_MouseEnter(object sender, EventArgs e)
        //{
        //    string toolTip;
        //    var local = gridView1.FocusedValue;
        //    var columnHandle = gridView1.FocusedColumn.Caption;
        //    if (columnHandle == "证卡大类")
        //    {
        //        toolTip = "1:人员;2 :车辆;3:物品;4:访客";

        //        //toolTip=GetToolTip();//这里写方法设置toolTip
        //        this.gridView1.SelectCells.ToolTipText = toolTip;
        //    }
        //    if (e.ColumnIndex == 9 && e.RowIndex >= 0)
        //    {
        //        toolTip = "0:未知;1:通过;2:未通过";

        //        //toolTip=GetToolTip();//这里写方法设置toolTip
        //        this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = toolTip;
        //    }
        //    if (e.ColumnIndex == 4 && e.RowIndex >= 0)
        //    {
        //        toolTip = "0-未知,1-制证申请,2-证卡信息录入,3-证卡资料审核,4-证卡打印,5-证卡授权,6-证卡检测,7-证卡发放";
        //        //toolTip=GetToolTip();//这里写方法设置toolTip
        //        this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = toolTip;
        //    }
        //}

        //private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        //{

        //}
    }
}